<?php

namespace App\Admin\Controllers\Vote\Season;

use App\Admin\Actions\Batches\BatchDisable;
use App\Admin\Actions\Batches\BatchEnable;
use App\Admin\Actions\GoBackButton;
use App\Admin\Traits\WithUploads;
use App\Models\Gift;
use App\Models\Season;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;

class GiftController extends Controller
{
    use WithUploads;

    public function index(Content $content, Season $season): Content
    {
        return $content->header('礼物管理')
            ->description($season->title)
            ->body($this->grid($season));
    }

    public function grid(Season $season): Grid
    {
        $model = Gift::ofAdmin()
            ->where('season_id', $season->getKey())
            ->with(['season'])
            ->ordered();

        return Grid::make($model, function (Grid $grid) {
            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
                $grid->disableCreateButton();
            }

            $grid->tools([new GoBackButton()]);

            $grid->batchActions([
                new BatchEnable(Gift::class),
                new BatchDisable(Gift::class),
            ]);

            $grid->column('name', '礼物名称');
            $grid->column('cover')
                ->thumb();
            $grid->column('origin_price', '划线价格');
            $grid->column('price', '支付价格');
            $grid->column('polls', '增长票数');
            $grid->column('order')
                ->orderable();
            $grid->column('status')
                ->bool();
            $grid->column('created_at');
        });
    }

    public function create(Content $content, Season $season): Content
    {
        return $content->header('新增礼物')
            ->description($season->title)
            ->body($this->form($season));
    }

    public function form(Season $season): Form
    {
        return Form::make(Gift::ofAdmin(), function (Form $form) use ($season) {
            $form->hidden('season_id')
                ->value($season->getKey());

            $form->text('name', '礼物名称')
                ->required();
            $form->currency('origin_price', '划线价格')
                ->required()
                ->default(10);
            $form->currency('price', '支付价格')
                ->required()
                ->default(9.5);
            $form->number('polls', '增长票数')
                ->required()
                ->default(100);
            $form->switch('status')
                ->default(true);
            $this->cover($form)
                ->dimensions(['min_width' => 50, 'max_width' => 100])
                ->help('图片宽度请在50~100px之间')
                ->width(4);
        });
    }

    public function edit(Content $content, Season $season, int $id): Content
    {
        return $content->header('编辑礼物')
            ->description($season->title)
            ->body($this->form($season)->edit($id));
    }

    public function update(Season $season, int|string $id): JsonResponse
    {
        return $this->form($season)->update($id);
    }

    public function store(Season $season): JsonResponse
    {
        return $this->form($season)->store();
    }

    public function destroy(Season $season, int|string $id): JsonResponse
    {
        return $this->form($season)->destroy($id);
    }
}
